<template>
  <div>
    <navigation-bar>
      <div class="searchContent">
        <!--				<el-select-->
        <!--					v-model="periodNo"-->
        <!--					clearable-->
        <!--					placeholder="请选择周期号"-->
        <!--					size="mini"-->
        <!--					style="width: 150px; margin-right: 10px"-->
        <!--				>-->
        <!--					<el-option-->
        <!--						v-for="item in periodNoList"-->
        <!--						:key="item.value"-->
        <!--						:label="item.label"-->
        <!--						:value="item.value"-->
        <!--						width="150px"-->
        <!--					>-->
        <!--					</el-option>-->
        <!--				</el-select>-->
        <el-select v-model="sample_use_type" clearable placeholder="请选择使用类型" size="mini"
          style="width: 150px; margin-right: 10px">
          <el-option v-for="item in sample_use_typeList" :key="item.value" :label="item.label" :value="item.value"
            width="150px">
          </el-option>
        </el-select>
        <el-select v-model="sample_status" clearable placeholder="请选择样品状况" size="mini"
          style="width: 150px; margin-right: 10px">
          <el-option v-for="item in sample_statusList" :key="item.value" :label="item.label" :value="item.value"
            width="150px">
          </el-option>
        </el-select>
        <custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
        <custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
        <!--        <custom-button-->
        <!--          type="add"-->
        <!--          @addClick="addClick"-->
        <!--          style="margin-right: 10px"-->
        <!--        ></custom-button>-->
        <custom-button type="add" @addClick="yscClick" title="预生成标签" style="margin-right: 10px"></custom-button>
        <custom-button type="download" @downloadClick="downloadClick" title="导出标签"
          style="margin-right: 10px"></custom-button>
        <custom-button type="print" @printClick="printClick" title="打印标签"></custom-button>
      </div>
    </navigation-bar>
    <el-row>
      <el-col :span="6">
        <div style="width: 98%">
          <single-table :dataSource="leftDataSource" :tableColumns="leftTableColumns"
            @singleHandleSelectionChange="singleHandleSelectionChange"></single-table>
        </div>
      </el-col>
      <el-col :span="18">
        <table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" :pageSize="pageSize"
          :pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" @editRow="editRow" @viewRow="viewRow"
          @deleteRow="deleteRow" selection isSizes @handleSelectionChange="handleSelectionChange"
          @handleSizeChange="handleSizeChange"></table-index>
      </el-col>
    </el-row>
    <div v-if="dialogShow">
      <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
        <form-index :formConfig="formConfig" @submit="submit" :type="type">
          <!--  <div>这里还可以插入其它内容进行拓展</div> -->
        </form-index>
      </custom-dialog>
    </div>
  </div>
</template>

<script>
import { sampleInformationForm } from '@/assets/form/managementCenter/sampleManagement/sampleInformationForm'
import customDialog from '@/components/customComponents/customDialog'
import customButton from '@/components/customComponents/customForm/customButton'
import formIndex from '@/components/customComponents/customForm/formIndex'
import singleTable from '@/components/customComponents/customeTable/singleTable'
import tableIndex from '@/components/customComponents/customeTable/tableIndex'
import navigationBar from '@/components/customComponents/managementCenter/navigationBar'
import { AjaxCommon, printAddress } from '@/libs/apiCommon'
import { PopupCommon } from '@/libs/popupCommon'
import { SessionCommon } from '@/libs/sessionCommon'
import { Utility } from '@/libs/utility'

export default {
  components: {
    navigationBar,
    customButton,
    tableIndex,
    singleTable,
    customDialog,
    formIndex,
  },
  data() {
    return {
      rightSelectIds: [],
      expSampleidList: [],
      periodNo: '',
      periodNoList: [
        {
          label: '第一周期',
          value: '1',
        },
        {
          label: '第二周期',
          value: '2',
        },
        {
          label: '第三周期',
          value: '3',
        },
        {
          label: '第四周期',
          value: '4',
        },
        {
          label: '第五周期',
          value: '5',
        },
      ],
      sample_use_type: '',
      sample_use_typeList: Utility.getDictList('sample_use_type'),
      sample_status: '',
      sample_statusList: Utility.getDictList('sample_status'),
      leftSelectIds: [],
      leftDataSource: [],
      leftTableColumns: [
        {
          prop: 'typeName',
          label: '试验样品名称',
          twoColumns: ['typeName', 'testIndex'],
        },
      ],
      // table 相关参数  begin
      dataSource: [],
      tableColumns: [
        {
          prop: 'experimentalPeriod',
          label: '试验周期',
        },
        {
          prop: 'labelName',
          label: '样品标签名称',
          width: 120,
        },
        {
          prop: 'labelCode',
          label: '样品标签编码',
          width: 120,
        },
        {
          prop: 'useType',
          label: '使用类型',
          dictName: 'sample_use_type',
        },
        {
          prop: 'sampleCollectionPoint',
          label: '样品采集点',
          width: 100,
        },
        {
          prop: 'randomNo',
          label: '随机号',
        },
        {
          prop: 'sampleStatus',
          label: '样品状况',
          width: 120,
          dictName: 'sample_status',
        },
        {
          prop: 'lceStoreBox',
          label: '存放冻存盒',
          width: 100,
        },
        {
          prop: 'storeRefrigerator',
          label: '存放冰箱',
          width: 120,
        },
        {
          prop: 'associationStatus',
          label: '关联状态',
          width: 120,
        },
        {
          prop: 'firstPrintTime',
          label: '首次打印时间',
          width: 140,
        },
        {
          prop: 'againPrintTime',
          label: '再次打印时间',
          width: 140,
        },
        {
          prop: 'printNum',
          label: '打印次数',
        },
      ],
      btnList: [],
      pageSize: 20,
      pageCount: 100,
      pageNo: 1,
      // table 相关参数  end
      // 弹窗是否显示
      dialogShow: false,
      // 弹窗标题 (新增修改不同)
      dialogTitle: '',
      // form的配置信息
      formConfig: sampleInformationForm,
      type: 'add',
    }
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e
      this.initPage()
    },
    downloadClick() {
      PopupCommon.notify('info', '暂无')
    },
    printClick() {
      let self = this
      var useTypeList = []
      if (self.sample_use_type.split(',').length > 1) {
        for (var i = 0; i < self.sample_use_type.split(',').length; i++) {
          useTypeList.push(self.sample_use_type.split(',')[i])
        }
      } else {
        if (self.sample_use_type === '') {
          useTypeList = []
        } else {
          useTypeList = [self.sample_use_type]
        }
      }
      let urlAdd = `${printAddress}/ccdc/tmrreport/pdf/show?1=1&_u=file:yangpingbqV1.tmrreport.xml&_i=1&_r=1
`
      if (this.rightSelectIds.length > 0) {
        let ids = this.rightSelectIds
          .map((t) => {
            return t.id
          })
          .join(',')
        if (this.rightSelectIds.length < 2) {
          urlAdd += '&id=' + ids
        } else {
          urlAdd += '&ids=' + ids
        }
      }
      let projectTestperiod = window.sessionStorage.getItem('projectTestperiod')
      if (projectTestperiod) {
        projectTestperiod = JSON.parse(projectTestperiod)
        urlAdd += `&pmId=${projectTestperiod.projectId}&expId=${projectTestperiod.testId
          }&periodNo=${projectTestperiod.periodId ? projectTestperiod.periodId : 1
          }`
      }

      console.log(urlAdd)
      window.open(urlAdd + '&type=' + useTypeList)
    },
    yscClick() {
      if (this.expSampleidList.length == 0) {
        PopupCommon.notify('info', '请选择至少一个试验样品')
        return
      }
      const self = this
      AjaxCommon('/ccdc/pm/sample/sampleInfo/preBuildLable', 'post', {
        expSampleidList: this.expSampleidList,
      })
        .then((res) => {
          if (res.data.code == 200) {
            PopupCommon.notify('success', '操作成功')
            self.initPage()
            self.dialogClose()
          } else {
            PopupCommon.notify('error', res.data.message)
          }
        })
        .catch((err) => {
          PopupCommon.notify('error', err.message)
        })
    },
    submit(d) {
      let self = this
      d.correctPlan = d.correctPlan ? 1 : 0
      if (d.id) {
        AjaxCommon('/ccdc/cfg/exp/ing/expIngCollectTimePoint/edit', 'post', d)
          .then((res) => {
            if (res.data.code == 200) {
              PopupCommon.notify('success', '操作成功')
              self.initPage()
              self.dialogClose()
            } else {
              PopupCommon.notify('error', res.data.message)
            }
          })
          .catch((err) => {
            PopupCommon.notify('error', err.message)
          })
      } else {
        delete d.id
        AjaxCommon('/ccdc/cfg/exp/ing/expIngCollectTimePoint/add', 'post', d)
          .then((res) => {
            if (res.data.code == 200) {
              PopupCommon.notify('success', '操作成功')
              self.initPage()
              self.dialogClose()
            } else {
              PopupCommon.notify('error', res.data.message)
            }
          })
          .catch((err) => {
            PopupCommon.notify('error', err.message)
          })
      }
    },
    dialogClose() {
      this.dialogShow = false
    },
    searchClick() {
      this.pageNo = 1
      this.initPage()
    },
    resetClick() {
      this.periodNo = ''
      this.sample_status = ''
      this.sample_use_type = ''
    },
    addClick() {
      PopupCommon.notify('info', '需求待定，需要讨论')
      // if (this.leftSelectIds.length != 1) {
      // 	PopupCommon.notify("info", "请勾选一个样品")
      // 	return
      // }
      // let formConfigItem = this.formConfig
      // formConfigItem.map((t) => {
      // 	Object.keys(t).map((res) => {
      // 		if (res == "fieldName") {
      // 			t.filedValue = ""
      // 		}
      // 	})
      // })
      // // formConfigItem.expId = this.leftSelectIds[0].id
      // this.formConfig = formConfigItem

      // this.type = "add"
      // this.dialogShow = true
      // this.dialogTitle =
      // 	"新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    // 分页点击事件
    pageChange(p) {
      this.pageNo = p
      this.initPage()
    },
    editRow(d) {
      this.type = 'edit'
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == 'fieldName') {
            t.filedValue = d.data[t[res]]
          }
        })
      })

      formConfigItem.correctPlan = d.correctPlan == 1

      this.formConfig = formConfigItem
      this.dialogShow = true
      this.dialogTitle =
        '编辑' + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    viewRow(d) {
      this.type = 'look'
      let formConfigItem = this.formConfig
      formConfigItem.map((t) => {
        Object.keys(t).map((res) => {
          if (res == 'fieldName') {
            t.filedValue = d.data[t[res]]
          }
        })
      })
      this.formConfig = formConfigItem
      this.dialogShow = true
      this.dialogTitle =
        '查看' + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
    },
    deleteRow(d) {
      const id = d.data.id
      let self = this
      AjaxCommon(
        '/ccdc/cfg/exp/ing/expIngCollectTimePoint/delete?id=' + id,
        'delete',
        { optReason: d.optReason }
      ).then((res) => {
        console.log(res, 'res...............')
        if (res.data.code == 200) {
          PopupCommon.notify('success', '删除成功')
          self.initPage()
          self.dialogClose()
        } else {
          PopupCommon.notify('error', res.data.message)
        }
      })
    },
    // 左侧选中事件
    singleHandleSelectionChange(d) {
      console.log(d)
      this.leftSelectIds = d
      this.expSampleidList = d ? d.map((t) => t.id) : []
      this.initPage()
    },
    // 右侧选中事件，todo hsy 打印标签时需要选中数据打印
    handleSelectionChange(d) {
      console.log(d)
      this.rightSelectIds = d
    },
    // 初始化事件
    initPage() {
      let self = this
      var useTypeList = []
      if (self.sample_use_type.split(',').length > 1) {
        for (var i = 0; i < self.sample_use_type.split(',').length; i++) {
          useTypeList.push(self.sample_use_type.split(',')[i])
        }
      } else {
        if (self.sample_use_type === '') {
          useTypeList = []
        } else {
          useTypeList = [self.sample_use_type]
        }
      }
      AjaxCommon('/ccdc/pm/sample/sampleInfo/listByPage', 'post', {
        expSampleIdList: this.expSampleidList,
        sampleStatus: this.sample_status,
        useTypeList: useTypeList,
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.data.code == 200) {
          self.dataSource = res.data.result.records
          self.pageCount = res.data.result.total
        } else {
          PopupCommon.notify('error', res.data.message)
        }
      })
    },
    initLeftData() {
      AjaxCommon('/ccdc/cfg/exp/root/expSample/listByPage', 'post', {}).then(
        (res) => {
          console.log(res, '.......................')
          if (res.data.code == 200) {
            this.leftDataSource = res.data.result.records
          } else {
            this.leftDataSource = []
            PopupCommon.notify('error', res.data.message)
          }
        }
      )
    },
  },
  mounted() {
    this.initLeftData()
    this.initPage()
  },
}
</script>

<style></style>
